#include "seedsdao.h"
#include "dbutil.h"
#ifdef LINUX_DB
#include <stdio.h>
#else
#include <QSqlQuery>
#include <QVariant>
#include <QSqlError>
#include <QSqlDatabase>
#endif
#include "dbutil.h"
#include <iostream>
using namespace std;
SeedsDao::SeedsDao()
{
}
bool SeedsDao::exec(QString str)
{
#ifdef LINUX_DB
    char sql[500];
    DbUtils dbUtils;
    sprintf(sql, str.toLocal8Bit().data());
    int result = dbUtils.exec(sql);
    dbUtils.freeRes();
    if (result == 0)
    {
        return true;
    }
    return false;
#else
    return false;
#endif
}
bool SeedsDao::insertSeeds(Seeds seeds)
{
    QString cd = seeds.getCreateTime().toString("yyyy-MM-dd hh:mm:ss");
    QString ud = seeds.getUpdateTime().toString("yyyy-MM-dd hh:mm:ss");
    QString str("insert into JYDW_SEEDS values(SEQ_JYDW_SEEDS_ID.nextval,'%1','%2',%3,%4,'%5','%6'\
                ,'%7',%8");

    str = str.arg(seeds.getIp(), seeds.getMask(), DbUtils::getStringFormInt(seeds.getPort()),
                  DbUtils::getStringFormInt(seeds.getVersion())
                  , seeds.getReadVisitor(), seeds.getWriteVisitor()
                  , seeds.getValidName(),
                  DbUtils::getStringFormInt(seeds.getAuthType()));
    str.append(",'%1',%2,'%3',to_date('%4','yyyy-mm-dd hh24:mi:ss'),%5,\
                       to_date('%6','yyyy-mm-dd hh24:mi:ss'),%7)");
    str = str.arg(seeds.getAuthPass()
                  , DbUtils::getStringFormInt(seeds.getPassType()),
                  seeds.getPriPass(),
                  cd
                  , DbUtils::getStringFormInt(seeds.getCreateId()),
                  ud,
                  DbUtils::getStringFormInt(seeds.getUpdateId()));

#ifdef LINUX_DB
    if (!exec(str))
    {
        DbUtils::ErrorLog("SeedsDao::insertSeeds", "SQL:" + str);
        return false;
    }
    return true;
#else
    QSqlQuery query;
    query.prepare(str);
    if (!query.exec())
    {
        DbUtils::ErrorLog("SeedsDao::insertSeeds", "SQL:" + str);
        return false;
    }
    return true;
#endif
}

bool SeedsDao::updateSeeds(Seeds seeds)
{
    QString str("update JYDW_SEEDS set IP='%1'");
    str.append(",SNMP_PORT=%2");
    str.append(",SNMP_VERSION=%3");
    str.append(",READ_COMMUNITY='%4'");
    str.append(",WRITE_COMMUNITY='%5'");
    str.append(",USM_USER='%6'");
    str.append(",AUTH_TYPE=%7");
    str.append(",MASK='%8'");
    str = str.arg(seeds.getIp(), DbUtils::getStringFormInt(seeds.getPort()),
                  DbUtils::getStringFormInt(seeds.getVersion())
                  , seeds.getReadVisitor(), seeds.getWriteVisitor()
                  , seeds.getValidName(),
                  DbUtils::getStringFormInt(seeds.getAuthType()));
    str = str.arg(seeds.getMask());
    str.append(",AUTH_PASS='%1'");
    str.append(",PRIVACY_TYPE=%2");
    str.append(",PRIVACY_PASS='%3'");
    str.append(",UPDATE_TIME=to_date('%4','yyyy-mm-dd hh24:mi:ss')");
    str.append("   where ID=%5");
    str = str.arg(seeds.getAuthPass(),
                  DbUtils::getStringFormInt(seeds.getPassType())
                  , seeds.getPriPass(), seeds.getUpdateTime().toString("yyyy-MM-dd hh:mm:ss")
                  , DbUtils::getStringFormInt(seeds.getId()));
#ifdef LINUX_DB
    if (!exec(str))
    {
        DbUtils::ErrorLog("SeedsDao::updateSeeds", "SQL:" + str);
        return false;
    }
    return true;
#else
    QSqlQuery query;
    query.prepare(str);
    if (!query.exec())
    {
        DbUtils::ErrorLog("SeedsDao::updateSeeds", "SQL:" + str);
        return false;
    }
    return true;
#endif
}
Seeds SeedsDao::getSeedsById(int id)
{
    QString str("select * from JYDW_SEEDS where ID=%1");
    str = str.arg(DbUtils::getStringFormInt(id));
#ifdef LINUX_DB
    Seeds seed;
    char sql[500];
    sprintf(sql, str.toLocal8Bit().data());
    DbUtils dbUtils;
    MYSQL_ROW result = dbUtils.startQuery(sql, sizeof(struct Seed_result), "SeedsDao::getSeedsById");
    while (result != NULL)
    {
        Seed_result *seed_result = (Seed_result *)result;
        seed.setId(seed_result->id);
        seed.setMask(seed_result->mask);
        seed.setIp(seed_result->ip);
        seed.setPort(seed_result->port);
        seed.setVersion(seed_result->version);
        seed.setReadVisitor(seed_result->readVisitor);
        seed.setWriteVisitor(seed_result->writeVisitor);
        seed.setValidName(seed_result->validName);
        seed.setAuthType(seed_result->authType);
        seed.setAuthPass(seed_result->authPass);
        seed.setPassType(seed_result->passType);
        seed.setPriPass(seed_result->priPass);
        seed.setCreateTime(DbUtils::getTimeFromLong(seed_result->createTime));
        //seed.setCreateId(seed_result->createId);
        seed.setUpdateTime(DbUtils::getTimeFromLong(seed_result->createTime));
        //seed.setUpdateId(seed_result->updateId);
        break;
    }
    dbUtils.freeRes();
    return seed;
#else
    Seeds seed;
    QSqlQuery query;
    query.prepare(str);
    if (!query.exec())
    {
        DbUtils::ErrorLog("SeedsDao::getSeedsById", "SQL:" + str);
    }
    while (query.next())
    {
        seed.setId(query.value(0).toInt());
        seed.setIp(query.value(1).toString());
        seed.setMask(query.value(2).toString());
        seed.setPort(query.value(3).toInt());
        seed.setVersion(query.value(4).toInt());
        seed.setReadVisitor(QString::fromLocal8Bit(query.value(5).toByteArray()));
        seed.setWriteVisitor(QString::fromLocal8Bit(query.value(6).toByteArray()));
        seed.setValidName(QString::fromLocal8Bit(query.value(7).toByteArray()));
        seed.setAuthType(query.value(8).toInt());
        seed.setAuthPass(query.value(9).toString());
        seed.setPassType(query.value(10).toInt());
        seed.setPriPass(query.value(11).toString());
        seed.setCreateTime(query.value(12).toDateTime());
        seed.setCreateId(query.value(13).toInt());
        seed.setUpdateTime(query.value(14).toDateTime());
        seed.setUpdateId(query.value(15).toInt());
        break;
    }
    return seed;
#endif
}
QList<Seeds *>  SeedsDao::getList()
{
    QString str("select * from JYDW_SEEDS ");
#ifdef LINUX_DB
    QList<Seeds *> list;
    char sql[500];
    sprintf(sql, str.toLocal8Bit().data());
    DbUtils dbUtils;
    MYSQL_ROW result = dbUtils.startQuery(sql, sizeof(struct Seed_result), "SeedsDao::getList");
    while (result != NULL)
    {
        Seed_result *seed_result = (Seed_result *)result;
        Seeds *seed = new Seeds();
        seed->setId(seed_result->id);
        seed->setIp(seed_result->ip);
        seed->setMask(seed_result->mask);
        seed->setPort(seed_result->port);
        seed->setVersion(seed_result->version);
        seed->setReadVisitor(seed_result->readVisitor);
        seed->setWriteVisitor(seed_result->writeVisitor);
        seed->setValidName(seed_result->validName);
        seed->setAuthType(seed_result->authType);
        seed->setAuthPass(seed_result->authPass);
        seed->setPassType(seed_result->passType);
        seed->setPriPass(seed_result->priPass);
        seed->setCreateTime(DbUtils::getTimeFromLong(seed_result->createTime));
        // seed->setCreateId(seed_result->createId);
        seed->setUpdateTime(DbUtils::getTimeFromLong(seed_result->createTime));
        // seed->setUpdateId(seed_result->updateId);
        list.push_back(seed);
        result = dbUtils.getNextRow();
    }
    dbUtils.freeRes();
    return list;
#else
    QList<Seeds *> list;
    QSqlQuery query;
    query.prepare(str);
    bool sr = query.exec();
    if (!sr)
    {
        DbUtils::ErrorLog("SeedsDao::getList", "SQL:" + str);
    }

    while (query.next())
    {
        Seeds *seed = new Seeds();
        seed->setId(query.value(0).toInt());
        seed->setIp(query.value(1).toString());
        seed->setMask(query.value(2).toString());
        seed->setPort(query.value(3).toInt());
        seed->setVersion(query.value(4).toInt());
        seed->setReadVisitor(QString::fromLocal8Bit(query.value(5).toByteArray()));
        seed->setWriteVisitor(QString::fromLocal8Bit(query.value(6).toByteArray()));
        seed->setValidName(QString::fromLocal8Bit(query.value(7).toByteArray()));
        seed->setAuthType(query.value(8).toInt());
        seed->setAuthPass(query.value(9).toString());
        seed->setPassType(query.value(10).toInt());
        seed->setPriPass(query.value(11).toString());
        seed->setCreateTime(query.value(12).toDateTime());
        seed->setCreateId(query.value(13).toInt());
        seed->setUpdateTime(query.value(14).toDateTime());
        seed->setUpdateId(query.value(15).toInt());
        list.push_back(seed);
    }
    return list;
#endif
}
bool SeedsDao::deleteSeed(int seedId)
{
    QString str("delete from  JYDW_SEEDS where id=%1");
    str = str.arg(DbUtils::getStringFormInt(seedId));
#ifdef LINUX_DB
    if (!exec(str))
    {
        DbUtils::ErrorLog("SeedsDao::deleteSeed", "SQL:" + str);
        return false;
    }
#else
    QSqlQuery query;
    query.prepare(str);
    if (!query.exec())
    {
        DbUtils::ErrorLog("SeedsDao::deleteSeed", "SQL:" + str);
        return false;
    }
#endif
    return true;
}
